package middleware

import (
	"net/http"
	"shuqi_novel/pkg"
)

type UserAgentMiddleware struct {
}

func NewUserAgentMiddleware() *UserAgentMiddleware {
	return &UserAgentMiddleware{}
}

func (m *UserAgentMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		// TODO generate middleware implement function, delete after code implementation

		// Passthrough to next handler if need
		token := r.Header.Get("token")
		getToken, err := pkg.GetToken(token)
		if err != nil {
			return
		}
		r.ParseForm()
		r.Form.Set("authorId", getToken["user"].(string))
		next(w, r)
	}
}
